package com.example.controller;

import com.example.domain.Course;
import com.example.service.CourseService;
import com.example.util.EasyuiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 课程 -- 页面跳转
 */
@Controller
@Slf4j
@RequestMapping(value = "/course")
public class CourseController {
    
    @Autowired
    private CourseService courceSerivce;
    
    
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public void delete(@RequestParam(value = "courseid", required = true) int courseid) {
        courceSerivce.delete(courseid);
    }
    
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestParam(value = "name", required = true) String name) {
        final int id = courceSerivce.add(name);
        if (id > 0)
            return "success";
        return "error";
    }
    
    
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public EasyuiResult<Course> list(@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
                                     @RequestParam(value = "order", required = false, defaultValue = "ASC") String order) {
        log.info("课程列表,参数为 ,sort={},order={}", sort, order);
        
        return courceSerivce.getAllList(sort, order);
    }
    
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @ResponseBody
    public List<Course> allCourses() {
        log.info("课程列表,参数为");
        
        return courceSerivce.getAllCourses();
    }
    
    
}
